const { successModel, errorModel, lackModel } = require("../utils/resModel");
const { getUserInfo, register, updateToken } = require("../controller/userController")
const checkData = require("../utils/checkData");
const cryptoModel = require("../utils/cryptoModel");

const userRouter = async (req, res) => {
    /**
     * 
     * @api {GET} api/user/getUserInfo 用户登录
     * @apiName 用户登录
     * @apiGroup User
     * @apiVersion  1.0.0
     * 
     * @apiParam  {String} name 用户名
     * @apiParam  {String} phone 手机号码
     * @apiParam  {String} idcard 身份证号码
     * 
     * @apiParamExample  {type} Request-Example:
     * {
     *     name : "bobo",
     *     phone:"15102068480",
     *     idcard:"440809200005206897"
     * }
     * 
     * 
    * @apiSuccessExample {type} Success-Response:
    [
        {
            "is_appointment": 0,
            "name": "melon",
            "qrcode": null,
            "sessions_id": null,
            "sessions_time": null,
            "token": "00ffc126cd17818735513f77f8ba907d1606271634424"
        }
    ]
      * 
     */

    if (req.method === "GET" && req.path === "/api/user/getUserInfo") {
        let checkRes = checkData(req, "name", "phone", "idcard");
        if (typeof checkRes === "boolean") {
            let userRes = await getUserInfo(req.query);
            if (userRes.length) {
                // 获取手机号码转换token加密
                let phone = userRes[0].phone;
                delete userRes[0].phone;
                // 因为是phone生成token所以不返回手机号码

                userRes[0].token = cryptoModel(phone) + Date.now();
                //最好是到期的时间戳

                let updateRes = await updateToken(phone, userRes[0].token)
                if (updateRes['changedRows']) {
                    return successModel(userRes)
                } else {
                    return errorModel("Token生成失败")
                }
            }else{
                return errorModel("账户密码错误")
            }
        } else {
            return lackModel(`${checkRes}参数格式不正确`)
        }
    }

    /**
     * 
     * @api {POST} api/user/register 用户注册
     * @apiName 用户注册
     * @apiGroup User
     * @apiVersion  1.0.0
     * 
     * @apiParam  {String} name 用户名
     * @apiParam  {String} phone 手机号码
     * @apiParam  {String} idcard 身份证号码
     * 
     * @apiParamExample  {type} Request-Example:
     * {
     *     name : "bobo",
     *     phone:"15102068480",
     *     idcard:"440809200005206897"
     * }
     * 
     * @apiSuccessExample {type} Success-Response:
    {
        "msg": "成功注册"
    }
     * 
     * 
     */


    if (req.method === "POST" && req.path === "/api/user/register") {


        // 把数据库返回的数据存储到userRes的变量
        // 如果数据库处理出错message(存储错误名/错误类型)就会存储错误类型,没有出错就为空
        let checkRes = checkData(req, "name", "phone", "idcard");
        // checkData放入req(请求的地方),还有有检查的参数
        if (typeof checkRes === "boolean") {
            req.body.token = cryptoModel(req.body.phone.toString()) + Date.now();
            let userRes = await register(req.body);
            if (!userRes['message']) {
                // return successModel("成功注册");
                return successModel({
                    "is_appointment": 0,
                    "name": req.body.name,
                    "qrcode": null,
                    "sessions_id": null,
                    "sessions_time": null,
                    "token": req.body.token
                });
            } else {
                return errorModel(userRes.message);
            }
        } else {
            return lackModel(`${checkRes}参数格式不正确`);
        }
    }
}

module.exports = userRouter;